package com.jt.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.service.UserService;
import com.jt.vo.SysResult;

import redis.clients.jedis.JedisCluster;

@RestController
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private JedisCluster jedisCluster;
	
	/**
	 * 实现用户信息校验
	 * JSONP调用,服务器端返回数据的要求:
	 * callback(json串)
	 */
	@RequestMapping("/check/{param}/{type}")
	public JSONPObject checkUser(@PathVariable String param,
								@PathVariable Integer type,
								String callback) {
		JSONPObject jSONPObject;
		try {
			boolean flag=userService.findCheckUser(param,type);
			jSONPObject=new JSONPObject(callback, SysResult.success(flag));
		} catch (Exception e) {
			e.printStackTrace();
			jSONPObject=new JSONPObject(callback, SysResult.fail());
		}
		return jSONPObject;
	}
	
	/**
	 * 实现用户信息查询回显
	 * url http://sso.jt.com/user/query/6201abe0d4032d9630f379cbf0191551
	 * /user/query/+ticket
	 */
	@RequestMapping("/query/{ticket}")
	public JSONPObject findUserByTicket(@PathVariable String ticket, String callback) {
		String userJSON=jedisCluster.get(ticket);//从缓存中查询登录数据
		JSONPObject jsonObject=null;
		if (StringUtils.isEmpty(userJSON)) {
			jsonObject=new JSONPObject(callback, SysResult.fail());
		}else {
			jsonObject=new JSONPObject(callback, SysResult.success(userJSON));//把用户信息返回给页面
		}
		return jsonObject;
	}
	
}
